<script type="text/javascript"> 
    dojo.require("dijit.Tooltip");
    dojo.require("dijit.Menu");   
    window.onload = function() {
        
        var bank = document.getElementById('BankId');
        var officer = document.getElementById('OfficerUserId');
        
        var group = document.getElementById('Group');        
        var addGroup = document.getElementById('AddGroup');
        var deleteGroup = document.getElementById('DeleteGroup');
        var exportGroup = document.getElementById('ExportGroup');
        var viewGroup = document.getElementById('ViewGroup');
        
        var client = document.getElementById('Client'); 
        var addClient = document.getElementById('AddClient');    
        var viewClient = document.getElementById('ViewClient');
        var editClient = document.getElementById('EditClient');
        
        officer.onchange = function () { updateOpts('CommunityId','/admin/json/get-officer-community-options/id/' + officer.value); return false; };
        
        group.onchange = function () { updateOpts('Client','/admin/json/get-group-client-options/id/' + group.value, groupOnChangeCallback); return false; };
        addGroup.onclick = function () { updateOpts('Group','/admin/json/add-group/id/' + bank.value, addGroupOnClickCallback); return false; };
        deleteGroup.onclick = function () { updateOpts('Group','/admin/json/deactivate-group/id/' + group.value, deleteGroupOnClickCallback); return false; };
        exportGroup.onclick = function () {  parent.main.location.href = '/admin/group/export/id/' + group.value; };             
        viewGroup.onclick = function () { parent.main.location.href = '/admin/group/view/id/' + group.value; }; 
        
        client.onchange = function () { clientButtonsDisabled(false); }; 
        addClient.onclick = function () { parent.main.location.href = '/admin/client/add/group/' + group.value; };                
        viewClient.onclick = function () { parent.main.location.href = '/admin/client/view/id/' + client.value; }; 
        editClient.onclick = function () { parent.main.location.href = '/admin/client/add/id/' + client.value; }; 
        
        if(bank.value) { initGroupButtons(); return false; }
    };  
    function updateOpts(tg,url,callback)
    {
        dojo.xhrGet({
            url: url,
            handleAs: "json",
            timeout: 5000,
            load: function(response, ioArgs) {                
                var obj = dojo.byId(tg);
                obj.options.length = 0;                
                for (index = 0; index < response.length; ++index) {
                    option = response[index];
                    obj.options.add(new Option(option.oT, option.oV));
                }   
                callback();
                return response;
            },
            error: function(response, ioArgs) {
                //alert('Failed xhrGet');
                return response;
            }
        });
    }
    function updateAddGroupStatus()
    {
        var group = document.getElementById('Group');
        var addGroup = document.getElementById('AddGroup');
        var deleteGroup = document.getElementById('DeleteGroup');
        if(group.options.length >= <?php echo Zend_Registry::get('HT_Config')->portfolio->group->max  ?>) {
            addGroup.disabled = true;
        } else {
            addGroup.disabled = false;
        }      
        deleteGroup.disabled = true;
    }
    function clientButtonsDisabled(status)
    {
        var group = document.getElementById('Group');
        if(!group.value) { return; }
        var client = document.getElementById('Client');
        var viewClient = document.getElementById('ViewClient');
        var addClient = document.getElementById('AddClient'); 
        var editClient = document.getElementById('EditClient'); 
        var deactivateClient = document.getElementById('DeactivateClient');    
        viewClient.disabled = status;    
        editClient.disabled = status;    
        deactivateClient.disabled = status;
        if (client.options.length < <?php echo Zend_Registry::get('HT_Config')->portfolio->client->max  ?>) {
            if(group.value) {
                addClient.disabled = status;
            }
        } else {   
            addClient.disabled = true; 
        }
        return;
    }
    /* callback functions */
    function groupOnChangeCallback() 
    {
        var client = document.getElementById('Client');
        var addClient = document.getElementById('AddClient');
        var deleteGroup = document.getElementById('DeleteGroup');
        var exportGroup = document.getElementById('ExportGroup');
        var viewGroup = document.getElementById('ViewGroup');
        if (client.options.length === 0) { 
            deleteGroup.disabled = false; 
            exportGroup.disabled = true;
            viewGroup.disabled = true; 
        } else { 
            deleteGroup.disabled = true;
            exportGroup.disabled = false;
            viewGroup.disabled = false; 
        }
        if (client.options.length < <?php echo Zend_Registry::get('HT_Config')->portfolio->client->max  ?>) {
            addClient.disabled = false;
        }
        clientButtonsDisabled(true);
    }
    function addGroupOnClickCallback() {
        var group = document.getElementById('Group'); 
        var client = document.getElementById('Client');
        var deleteGroup = document.getElementById('DeleteGroup');
        var exportGroup = document.getElementById('ExportGroup');
        var viewGroup = document.getElementById('ViewGroup');
        group.options[group.options.length - 1].selected = true;
        client.options.length = 0;
        updateAddGroupStatus(); 
        clientButtonsDisabled(true); 
        exportGroup.disabled = true; 
        viewGroup.disabled = true;
        deleteGroup.disabled = false; 
    }
    function deleteGroupOnClickCallback() { 
        var group = document.getElementById('Group'); 
        group.options[0].selected = true;
        updateOpts('Client','/admin/json/get-group-client-options/id/' + group.options[0].value, groupOnChangeCallback);
        updateAddGroupStatus(); 
        clientButtonsDisabled(true);
    }
    function initGroupButtons() {
        var group = document.getElementById('Group'); 
        if(!group.value && (group.options.length > 0)) { group.options[0].selected = true; }  
        updateOpts('Client','/admin/json/get-group-client-options/id/' + group.value, groupOnChangeCallback);
        updateAddGroupStatus(); 
        initClientButtons();
    }
    function initClientButtons() {
        var group = document.getElementById('Group'); 
        var client = document.getElementById('Client');
        if(group.value && !client.value) {
            if(client.options.length > 0) { client.options[0].selected = true; }
            clientButtonsDisabled(false);
        }
    }
</script>

<table>
    <tr>
        <td valign="top">
            <?php echo $this->form ?>
        </td>
        <td valign="top">
            <?php echo $this->formTool ?>
        </td>
    </tr>
</table>

